package com.ming.springboot.security;

import com.alibaba.fastjson.JSON;
import com.ming.springboot.data.Result;
import com.ming.springboot.data.ResultCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by mqsi on 2022/5/24 17:16
 */
@Component
public class AjaxAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
		Result res = Result.fail(ResultCode.NEED_AUTHORIZED.getCode(), "Need Authorities");
		response.getWriter().write(JSON.toJSONString(res));
	}
}
